package com.smallkingson.ylib.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.smallkingson.ylib.R;
import com.smallkingson.ylib.view.listener.YPositionOnClickListener;
import com.smallkingson.ylib.view.utils.YViews;


public class YTab extends LinearLayout {

	public OnTabSeleteListener onTabSeleteListener;

	private ArrayList<View> tabs;

	private int selectedIndex;

	boolean isCreated;

	boolean hideable;

	private int clickIndex;

	public YTab(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.yr);
		selectedIndex = array.getInt(R.styleable.yr_selected, 0);
		hideable = array.getBoolean(R.styleable.yr_hideable, false);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);

		if (!isCreated) {
			onCreate();
			isCreated = true;
		}
	}

	private void onCreate() {

		tabs = new ArrayList<View>();

		setGravity(Gravity.CENTER_VERTICAL);

		int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {

			final View childView = getChildAt(i);

			if (!(childView instanceof ViewGroup)) {
				continue;
			}

			tabs.add(childView);

			LayoutParams layoutParams = (LayoutParams) childView.getLayoutParams();

			layoutParams.weight = 1;

			childView.setLayoutParams(layoutParams);

			childView.setClickable(true);

			final YPositionOnClickListener onClickListenr = new YPositionOnClickListener(i) {

				@Override
				public void onClick(View v) {

					clickIndex = this.position;
					// 设置imageView和TextView为选中状态
					if (hideable) {
						if (selectedIndex == this.position) {

							selectedIndex = -1;
						} else {
							selectedIndex = this.position;
						}
					} else {

						selectedIndex = this.position;
					}

					setTab(selectedIndex);

					if (onTabSeleteListener != null) {
						onTabSeleteListener.onTabClicked(this.position, selectedIndex > -1 ? true : false, v);
					}
				}
			};
			childView.setOnClickListener(onClickListenr);
			YViews.traverseView(childView, new YViews.ViewHandler() {

				@Override
				public void handlerView(View v) {
					v.setOnClickListener(onClickListenr);
				}
			});
		}
		setTab(selectedIndex);
	}

	public void setDismissAll() {
		selectedIndex = -1;
		setTab(selectedIndex);
		if (onTabSeleteListener != null) {
			onTabSeleteListener.onTabClicked(clickIndex, selectedIndex > -1 ? true : false, null);
		}
	}

	public void setTab(int index) {

		for (View view : tabs) {
			setChildViewSelect(view, false);
		}
		if (index < 0) {
			return;
		}
		View childView = getChildAt(index);

		setChildViewSelect(childView, true);

	}

	private void setChildViewSelect(final View childView, boolean select) {
		if (childView instanceof ViewGroup) {

			ViewGroup tabViewGroup = (ViewGroup) childView;

			int tabChildCount = tabViewGroup.getChildCount();

			for (int j = 0; j < tabChildCount; j++) {

				View tabChildView = tabViewGroup.getChildAt(j);

				tabChildView.setSelected(select);
			}
		}
	}

	public void setOnTabSeleteListener(OnTabSeleteListener onTabSeleteListener) {
		this.onTabSeleteListener = onTabSeleteListener;
	}

	public static interface OnTabSeleteListener {

		void onTabClicked(int index, boolean isShow, View v);
	}
}
